package com.imc.platform.base.org.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.imc.core.config.feign.MyPageImpl;
import com.imc.core.config.redis.RedisTimeClean;
import com.imc.core.utils.TypeConversion;
import com.imc.platform.base.notice.dao.INoticeMapper;
import com.imc.platform.base.notice.pojo.Notice;
import com.imc.platform.base.org.dao.IOrgMapper;
import com.imc.platform.base.org.pojo.*;
import com.imc.platform.base.org.service.IOrgService;
import com.imc.platform.base.user.dao.mapper.IUserMapper;
import com.imc.platform.base.user.pojo.User;
import com.imc.platform.base.user.pojo.UserAttach;
import com.imc.platform.base.user.pojo.UserDesc;
import lombok.NoArgsConstructor;
import lombok.val;
import org.apache.commons.lang.StringEscapeUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/*
*类描述：组织机构
*开发者：ZL
*创建时间：2018-8-3 14:57:32
*/
@Service
public class OrgServiceImpl implements IOrgService, RedisTimeClean {
    @Autowired
    INoticeMapper iNoticeMapper;
    @Autowired
    IOrgMapper iOrgMapper;
    @Autowired
    IUserMapper userMapper;
    @Autowired
    TypeConversion typeConversion;
    @Autowired
    INoticeMapper noticeMapper;

    @Override
    public PageInfo<Org> queryOrgPage(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum,pageSize,true);
        PageInfo d= new PageInfo<>(iOrgMapper.queryOrg());
        return d;
    }

    @Override
    public List<Org> queryOrg() {
        return iOrgMapper.queryOrg();
    }

    @Override
    public int updateOrgName(Org org) {
        return iOrgMapper.updateOrgName(org);
    }

    @Override
    @CacheEvict(cacheNames ={"proFile","orgList"}, allEntries=true)
    public int delOrgbyId(String treeid) {
        return iOrgMapper.delOrgbyId(treeid);
    }

    @Override
    @CacheEvict(cacheNames ={"proFile","orgList"}, allEntries=true)
    public int delOrgbyPid(String treeid) {
        return iOrgMapper.delOrgbyPid(treeid+"%");
    }

    @Override
    public int clearChildren(Map map) {
        return iOrgMapper.clearChildren(map);
    }

    @Override
    public Org queryOrgSn() {
        return iOrgMapper.queryOrgSn();
    }

    @Override
    public void addOrg(Org org) {
        iOrgMapper.addOrg(org);

    }

    /**
     * 方法描述: 修改机构简介
     * SJC 2018-9-10 20:17:39
     *
     */
    @Override
    public int updateOrgDesc(String orgId,String orgDesc){
        return  iOrgMapper.updateOrgDesc(orgId,orgDesc);
    }


    /**
     * 方法描述: 修改招聘简介
     * SJC 2018-9-10 20:17:39
     *
     */
    @Override
    public int updateRecruit(String id,String recruitInfo){
        return  iOrgMapper.updateRecruit(id,recruitInfo);
    }

    /**
     * 方法描述: 修改教研团队
     * SJC 2018-9-10 20:17:39
     *
     */
    @Override
    public int updateTeam(String id,String teamInfo){
        return  iOrgMapper.updateTeam(id,teamInfo);
    }


    /**
     * 方法描述:新增招聘信息
     * SJC 2018-9-28 17:37:23
     *
     */
    @Override
    public int addOrgRecruit(String orgId,String desc){
        return  iOrgMapper.addOrgRecruit(orgId,desc);
    }

    /**
     * 方法描述:新增教研团队
     * SJC 2018-9-28 17:38:06
     *
     */
    @Override
    public int addOrgTeam(String orgId,String desc){
        return  iOrgMapper.addOrgTeam(orgId,desc);
    }

    @Override
    public String queryOrgId(String orgId) {
        return iOrgMapper.queryOrgId(orgId);
    }

    /**
     * 方法描述: 新增组织机构详情
     * SJC 2018-9-10 20:17:39
     *
     */
    @Override
    public int updateOrgDetail(String orgId,String orgName,String address,String coordinate){
        return iOrgMapper.updateOrgDetail(orgId,orgName,address,coordinate);
    }


    /**
     * 方法描述: 新增组织机构详情
     * SJC 2018-9-10 20:17:39
     *
     */
    @Override
    public int addOrgAttach(OrgAttach orgAttach){
        int count = iOrgMapper.addOrgAttach(orgAttach);
        return count;
    }

    @Override
    public void delOrgAttrch(String id, String imgUrl) {
       iOrgMapper.delOrgAttrch(id,imgUrl);
    }

    @Override
    public void updateOrgDet(String orgId, String orgDesc) {
        iOrgMapper.updateOrgDet(orgId,orgDesc);
    }


    /**
     * 方法描述:获取组织详情
     * SJC 2018-9-12 19:40:29
     *
     */
    @Override
    public List<Map<String, Object>> queryOrgInfos(String id){
        return iOrgMapper.queryOrgInfos(id);
    }


    /**
     * 方法描述:获取简介列表
     * SJC 2018-9-12 19:40:29
     *
     */
    @Override
    public MyPageImpl queryIntroduce(String name, String contentType, Pageable pageable){
        PageInfo info = new PageInfo();
        List<Map<String,Object>> data =new ArrayList<>();
        if(contentType.equals("teacherTeam")){//教研
            PageHelper.startPage(pageable.getPageNumber()+1, pageable.getPageSize());
            List<User> queryUserNoStudent= userMapper.queryUserNoStudent(name);
            info = new PageInfo <>(queryUserNoStudent);
            for (User user:queryUserNoStudent){
                data.add(typeConversion.objectToMap(user));
            }
        }else if(contentType.equals("schoolAddress")){
            List<Org> queryOrgAll=iOrgMapper.queryOrg();
            for (Org org:queryOrgAll){
                data.add(typeConversion.objectToMap(org));
            }
        }

        return new MyPageImpl(data, info.isIsLastPage());
    }

    /**
     * 方法描述:获取简介详情
     * SJC 2018-9-29 17:45:37
     *
    */
    @Override
    public List<Map<String,Object>> queryIntroduceInfo(String id,String contentType){
        Map<String,Object> map = new HashMap<>();
        List<Map<String,Object>>ntList=new ArrayList<>();
        if (contentType.equals("schoolSummary")){    //学校简介
            if(StringUtils.isEmpty(id)){
                id="1";
            }
            map.put("orgId",id);
            map.put("noticeType","1");
            Notice notice=noticeMapper.queryNoticeDet(map);
            if(StringUtils.isNotEmpty(notice.getContent())){
                  notice.setContent(notice.getContent());
                  Map<String,Object>retMap=new HashMap<>();
                  retMap.put("id",notice.getId());
                retMap.put("content",notice.getContent());
                retMap.put("title",notice.getTitle());
                retMap.put("rules",notice.getRules());
                retMap.put("partTitle",notice.getAddress());
                retMap.put("clickNum",notice.getClickNum());
                ntList.add(retMap);
            }
            return ntList;
        }else if(contentType.equals("enrolStudents")){//招生
            if(StringUtils.isEmpty(id)){
                id="1";
            }
            return iOrgMapper.queryEnrolStudentInfoToApi(id);
        }else if(contentType.equals("teacherTeam")){//教研
            List<Map<String,Object>>userList=new ArrayList<>();
            User user = userMapper.queryUserById(id);
            map.put("content",user.getUserDesc()==null?"":user.getUserDesc());
            userList.add(
                    map
            );
            return userList;
        }else if(contentType.equals("schoolAddress")){//我的地址
            Org org=iOrgMapper.queryOrgByOrgId(id);
            List<Map<String,Object>>orgList=new ArrayList<>();
            map.put("id",org.getId());
            map.put("orgName",org.getOrgName());
            map.put("address",org.getAddress());
            map.put("content",org.getContent());
            String longitude="";
            String latitude="";
            if(StringUtils.isNotEmpty(org.getCoordinate())){
                String[] tmp=org.getCoordinate().split(",");
                  longitude=tmp[0];
                  latitude=tmp[1];
            }
            map.put("longitude",longitude);
            map.put("latitude",latitude);
            orgList.add(map);
            return orgList;
        }else if(contentType.equals("teacherSys")){//教学体系
            if(StringUtils.isEmpty(id)){
                id="1";
            }
            map.put("orgId",id);
            map.put("noticeType","2");
            Notice notice=noticeMapper.queryNoticeDet(map);
            if(StringUtils.isNotEmpty(notice.getContent())){
                notice.setContent(notice.getContent());
                Map<String,Object>retMap=new HashMap<>();
                retMap.put("id",notice.getId());
                retMap.put("content",notice.getContent());
                retMap.put("title",notice.getTitle());
                retMap.put("rules",notice.getRules());
                retMap.put("partTitle",notice.getAddress());
                retMap.put("clickNum",notice.getClickNum());
                ntList.add(retMap);
            }
            return ntList;
        }else if(contentType.equals("rules")){
            Map<String,Object>parms=new HashMap<>();
            parms.put("id","");
            parms.put("noticeType","35");
            Notice notice=noticeMapper.queryNoticeDet(parms);
            Map<String,Object>retMap=new HashMap<>();
            retMap.put("id",notice.getId());
            retMap.put("content",notice.getContent());
            retMap.put("title",notice.getTitle());
            retMap.put("rules",notice.getRules());
            retMap.put("partTitle",notice.getAddress());
            retMap.put("clickNum",notice.getClickNum());
            ntList.add(retMap);
//            if(list.size()>0){
//                for(Object object : list){
//                    Notice notice=(Notice) object;
//                    Map<String,Object>retMap=new HashMap<>();
//                    retMap.put("id",notice.getId());
//                    retMap.put("content",notice.getContent());
//                    retMap.put("title",notice.getTitle());
//                    retMap.put("rules",notice.getRules());
//                    retMap.put("partTitle",notice.getAddress());
//                    retMap.put("clickNum",notice.getClickNum());
//                    ntList.add(retMap);
//                    String content=notice.getContent();
//                    if (!StringUtils.isEmpty(content) && StringUtils.isNotEmpty(parms.get("id").toString())){
//                        content= StringEscapeUtils.unescapeHtml(content);
//                        content=content.replaceAll("<[.[^<]]*>","");
//                        content=content.replaceAll("\n","").trim() ;
//                        int len=content.length() >=120 ? 120 : content.length()-1;
//                        notice.setContent(content.substring(0,len));
//                    }else {
//                        notice.setContent(content);
//                    }
//                    result.add(notice);
//                }
//            }
            return ntList;
//            if(StringUtils.isEmpty(id)){
//                id="1";
//            }
//            map.put("orgId",id);
//            map.put("noticeType","1");
//            Notice notice=noticeMapper.queryNoticeDet(map);
//            if(StringUtils.isNotEmpty(notice.getContent())){
//                notice.setContent(notice.getContent());

//            }
//            return ntList;
        }
        else {
            return null;
        }
    }

    /**
     * 方法描述:根据组织id获取附件列表
     * SJC 2018-9-12 19:44:59
     *
     */
    @Override
    public List<OrgAttach> queryOrgAttach(String orgId){
        return iOrgMapper.queryOrgAttach(orgId);
    }


    /**
     * 方法描述:新增招聘信息
     * SJC 2018-9-25 20:35:45
     *
     */
    @Override
    public int addRecruit(OrgRecruit orgRecruit){
        return iOrgMapper.addRecruit(orgRecruit);
    }


    /**
     * 方法描述:新增教研团队
     * SJC 2018-9-25 20:35:45
     *
     */
    @Override
    public int addTeam(OrgTeam team){
        return iOrgMapper.addTeam(team);
    }

    @Override
    public String queryOrgIdByUserId(String userId) {
        return iOrgMapper.queryOrgIdByUserId(userId);
    }

    @Override
    public String queryOrgNameByOrgId(String orgId) {
        return iOrgMapper.queryOrgNameByOrgId(orgId);
    }


    @Override
    public UserAttach queryTeacherDet(String userId) {
        List<UserAttach>queryTeacherDet=userMapper.queryUserDescAllByUserId(userId);
        UserAttach userAttach=new UserAttach();
        for(UserAttach attach:queryTeacherDet){
             if(StringUtils.isNotEmpty(attach.getContent())){
                 userAttach=attach;
             }
        }
        return userAttach;
    }

    @Override
    public void updateOrgBasic(Org org) {
        //一级节点
        if(org.getParentId().equals("0")){
            String upOrgSn=iOrgMapper.queryUpSn(org.getMoveObjId());
            if("1".equals(upOrgSn)){
                org.setSn("1");
            }else {
                if("prev".equals(org.getMoveType())){
                    org.setSn(String.valueOf(Integer.parseInt(upOrgSn)-1));
                }else if("next".equals(org.getMoveType())){
                    org.setSn(String.valueOf(Integer.parseInt(upOrgSn)+1));
                }else {
                    org.setSn(String.valueOf(Integer.parseInt(upOrgSn)+1));
                    org.setLevel(String.valueOf(Integer.parseInt(org.getLevel())+1));
                    org.setParentId(org.getMoveObjId());
                }
            }
        }else {
            String upOrgMaxSn=iOrgMapper.queryUpMaxSn(org.getMoveObjId());
            if(!"".equals(upOrgMaxSn) && null !=upOrgMaxSn){
                if("prev".equals(org.getMoveType())){
                    if("1".equals(upOrgMaxSn)){
                        org.setSn("1");
                    }else {
                        org.setSn(String.valueOf(Integer.parseInt(upOrgMaxSn)-1));
                    }
                }else if("next".equals(org.getMoveType())){
                    org.setSn(String.valueOf(Integer.parseInt(upOrgMaxSn)+1));
                }else {
                    org.setSn(String.valueOf(Integer.parseInt(upOrgMaxSn)+1));
                    org.setLevel(String.valueOf(Integer.parseInt(org.getLevel())+1));
                    org.setParentId(org.getMoveObjId());
                }
            }else {
                org.setSn("1");
            }
        }
        org.setLevel(String.valueOf(Integer.parseInt(org.getLevel())+1));
        iOrgMapper.updateOrgBasic(org);
    }

    @Override
    public String queryUpMaxSn(String parentId) {
        return iOrgMapper.queryUpMaxSn(parentId);
    }

    @Override
    public PageInfo<Map<String,Object>> queryLiveList(Pageable pageable,String orgId) {
        if(pageable != null){
            PageHelper.startPage(pageable.getPageNumber(),pageable.getPageSize());
        }
        val data= iOrgMapper.queryLiveList(orgId);
        PageInfo info=new PageInfo<>(data);
        return info;
    }

    @Override
    public int updateOrgLiveState(Map<String, Object> parms) {
        return iOrgMapper.updateOrgLiveState(parms);
    }

    @Override
    public PageInfo<Map<String,Object>> queryLiveAllList(Pageable pageable) {
        if(pageable != null){
            PageHelper.startPage(pageable.getPageNumber(),pageable.getPageSize());
        }
        val data= iOrgMapper.queryLiveAllList();
        PageInfo info=new PageInfo<>(data);
        return info;
    }

    @Override
    public OrgLive queryLiveByLiveId(String liveId) {
        return iOrgMapper.queryLiveByLiveId(liveId);
    }

    @Override
    public void addLive(OrgLive orgLive) {
        iOrgMapper.addLive(orgLive);
    }

    @Override
    public int updateLive(OrgLive orgLive) {
        return iOrgMapper.updateOrgLive(orgLive);
    }

    @Override
    public int delLiveByLiveId(String liveId) {
        iOrgMapper.delLiveByLiveId(liveId);
        return iOrgMapper.delLiveAllAtt(liveId);
    }

    @Override
    public void delLiveAtt(String liveId,String imgUrl) {
        iOrgMapper.delLiveAtt(liveId,imgUrl);
    }

    @CacheEvict(cacheNames ={"proFile","orgList"}, allEntries=true)
    public void timeClean() {}
}
